/* 
 * Copyright (C) 2013 Vitaljok
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package lv.llu.rembot.ctr.client;

import java.awt.Component;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import lv.llu.rembot.ctr.player.PlayerConfig;
import lv.llu.rembot.ctr.player.PlayerConfigSimpleImpl;

/**
 *
 * @author Vitaljok
 */
public class PlayerManagerJPanel extends javax.swing.JPanel {

    DefaultComboBoxModel<PlayerConfig> configModel;
    PlayerConfig currentConfig;

    /**
     * Creates new form PlayerManagerJPanel
     */
    public PlayerManagerJPanel() {
        initComponents();

        this.configModel = new DefaultComboBoxModel<>();
        this.jComboBoxConfigs.setModel(this.configModel);
        this.jComboBoxConfigs.setRenderer(new PlayerConfigRenderer());
        this.configModel.addElement(PlayerConfigSimpleImpl.getDefaultConfig());

        this.refreshConfigPanel();
    }

    private void refreshConfigPanel() {
        Boolean isValid = this.currentConfig != null;
        if (isValid) {
            this.jTextFieldDescr.setText(this.currentConfig.getDescription());
            this.jTextAreaConfig.setText(this.currentConfig.getConfigText());
        }

        this.jPanelConfigDetails.setVisible(isValid);
        this.jButtonRename.setEnabled(isValid);
        this.jButtonDelete.setEnabled(isValid);
        this.jButtonStart.setEnabled(isValid);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBoxConfigs = new javax.swing.JComboBox();
        jButtonNew = new javax.swing.JButton();
        jButtonRename = new javax.swing.JButton();
        jButtonDelete = new javax.swing.JButton();
        jButtonStart = new javax.swing.JButton();
        jButtonStop = new javax.swing.JButton();
        jPanelConfigDetails = new javax.swing.JPanel();
        jLabelDescr = new javax.swing.JLabel();
        jTextFieldDescr = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaConfig = new javax.swing.JTextArea();

        jComboBoxConfigs.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBoxConfigs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxConfigsActionPerformed(evt);
            }
        });

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("lv/llu/rembot/ctr/language"); // NOI18N
        jButtonNew.setText(bundle.getString("PLAYER_CONFIG_NEW")); // NOI18N
        jButtonNew.setFocusable(false);
        jButtonNew.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonNew.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewActionPerformed(evt);
            }
        });

        jButtonRename.setText(bundle.getString("PLAYER_CONFIG_RENAME")); // NOI18N
        jButtonRename.setFocusable(false);
        jButtonRename.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonRename.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonRename.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRenameActionPerformed(evt);
            }
        });

        jButtonDelete.setText(bundle.getString("PLAYER_CONFIG_DELETE")); // NOI18N
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteActionPerformed(evt);
            }
        });

        jButtonStart.setText(bundle.getString("PLAYER_START")); // NOI18N

        jButtonStop.setText(bundle.getString("PLAYER_STOP")); // NOI18N

        jLabelDescr.setText(bundle.getString("PLAYER_CONFIG_LABEL_DESCRIPTION")); // NOI18N

        jTextFieldDescr.setToolTipText("");
        jTextFieldDescr.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldDescrKeyTyped(evt);
            }
        });

        jTextAreaConfig.setColumns(20);
        jTextAreaConfig.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        jTextAreaConfig.setRows(5);
        jScrollPane1.setViewportView(jTextAreaConfig);

        javax.swing.GroupLayout jPanelConfigDetailsLayout = new javax.swing.GroupLayout(jPanelConfigDetails);
        jPanelConfigDetails.setLayout(jPanelConfigDetailsLayout);
        jPanelConfigDetailsLayout.setHorizontalGroup(
            jPanelConfigDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelConfigDetailsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelConfigDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(jPanelConfigDetailsLayout.createSequentialGroup()
                        .addComponent(jLabelDescr)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldDescr)))
                .addContainerGap())
        );
        jPanelConfigDetailsLayout.setVerticalGroup(
            jPanelConfigDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelConfigDetailsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelConfigDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDescr)
                    .addComponent(jTextFieldDescr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jComboBoxConfigs, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonNew)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonRename)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 163, Short.MAX_VALUE)
                .addComponent(jButtonStart)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonStop)
                .addContainerGap())
            .addComponent(jPanelConfigDetails, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jComboBoxConfigs, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonNew)
                    .addComponent(jButtonRename)
                    .addComponent(jButtonStart)
                    .addComponent(jButtonStop)
                    .addComponent(jButtonDelete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelConfigDetails, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewActionPerformed

        String name = JOptionPane.showInputDialog(this,
                java.util.ResourceBundle.getBundle("lv/llu/rembot/ctr/language").getString("PLAYER_CONFIG_NEW_MSG"),
                java.util.ResourceBundle.getBundle("lv/llu/rembot/ctr/language").getString("PLAYER_CONFIG_NEW_TITLE"),
                JOptionPane.QUESTION_MESSAGE);

        if (name != null && !name.trim().isEmpty()) {
            PlayerConfig newConfig = new PlayerConfigSimpleImpl(name);

            this.configModel.addElement(newConfig);
            this.configModel.setSelectedItem(newConfig);
        }
    }//GEN-LAST:event_jButtonNewActionPerformed

    private void jButtonRenameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRenameActionPerformed

        if (this.currentConfig != null) {
            String name = (String) JOptionPane.showInputDialog(this,
                    java.util.ResourceBundle.getBundle("lv/llu/rembot/ctr/language").getString("PLAYER_CONFIG_RENAME_MSG"),
                    java.util.ResourceBundle.getBundle("lv/llu/rembot/ctr/language").getString("PLAYER_CONFIG_RENAME_TITLE"),
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    null,
                    this.currentConfig.getName());

            if (name != null && !name.trim().isEmpty()) {
                this.currentConfig.setName(name);
                this.configModel.setSelectedItem(this.currentConfig);
            }
        }
    }//GEN-LAST:event_jButtonRenameActionPerformed

    private void jComboBoxConfigsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxConfigsActionPerformed
        this.currentConfig = (PlayerConfig) this.configModel.getSelectedItem();
        this.refreshConfigPanel();
    }//GEN-LAST:event_jComboBoxConfigsActionPerformed

    private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
        if (this.currentConfig != null) {

            if (JOptionPane.showConfirmDialog(
                    this,
                    java.util.ResourceBundle.getBundle("lv/llu/rembot/ctr/language").getString("PLAYER_CONFIG_DELETE_MSG"),
                    java.util.ResourceBundle.getBundle("lv/llu/rembot/ctr/language").getString("PLAYER_CONFIG_DELETE_TITLE"),
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION) {
                this.configModel.removeElement(this.currentConfig);
            }
        }
    }//GEN-LAST:event_jButtonDeleteActionPerformed

    private void jTextFieldDescrKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldDescrKeyTyped
        this.currentConfig.setDescription(this.jTextFieldDescr.getText());
    }//GEN-LAST:event_jTextFieldDescrKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JButton jButtonNew;
    private javax.swing.JButton jButtonRename;
    private javax.swing.JButton jButtonStart;
    private javax.swing.JButton jButtonStop;
    private javax.swing.JComboBox jComboBoxConfigs;
    private javax.swing.JLabel jLabelDescr;
    private javax.swing.JPanel jPanelConfigDetails;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaConfig;
    private javax.swing.JTextField jTextFieldDescr;
    // End of variables declaration//GEN-END:variables

    class PlayerConfigRenderer extends BasicComboBoxRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

            if (value != null) {
                PlayerConfig config = (PlayerConfig) value;
                this.setText(config.getName());
            }

            return this;
        }
    }
}